function HandlePageSumIncome(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options
  
  this._createElements();
};

HandlePageSumIncome.prototype._createElements = function() {
  var _self = this;
  this._popupBox = new PopupBox();
  this._search = DlmanageUtils.search();
  
  tr = this._gui.mains.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.backgroundColor = '#fff';
  td.colSpan = 13;
  td.appendChild(DOMUtils.getLoadingImage());
  
  this._loadData();
};

HandlePageSumIncome.prototype._loadData = function() {
  this._customers = null;
  this._payments = null;
  this._dateZones = null;
  this._retrieveCustomers(1);
  this._retrieveDateZones();
};

HandlePageSumIncome.prototype._verifyData = function() {
  if (this._customers && this._dateZones) {
    this._updateElements();
  }
};

HandlePageSumIncome.prototype._retrieveCustomers = function(page, condition, from, to) {
  var _self = this;
  var args = '&c=' + ((condition) ?  condition : '1 = 1') + '&from=' + ((from) ? from : JSON.stringify(SimpleDate.fromStamp(SimpleDate.toStamp(this._now)- 31536000))) + '&to=' + ((to) ? to : JSON.stringify(this._now));
  new RequestUtils()._mysql('iesumincome', args, function(result, params) { _self._customers = result;
                                                                             _self._verifyData.call(_self);
                                                                           }, null);
};

HandlePageSumIncome.prototype._retrieveDateZones = function(page, condition, date, from, to, pos) {
  var _self = this;
   var arg ="&c=d.page=\'" + this._options.template + "\' ";
  /*new RequestUtils()._read('datezone', null, 'd.page=\'' + this._options.template + '\'', null, null, null, null, function(result, params) { _self._dateZones = result.data;
                                                                                                                                            _self._verifyData.call(_self);
                                                                                                                                          }, null);*/
   new RequestUtils()._mysql('datezone', arg, function(result, params) { _self._dateZones = result.data;
                                                                          _self._parameters = result;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php',pos: (pos) ? pos : null });   
};

HandlePageSumIncome.prototype._updateElements = function() {
  var _self = this;
  DOMUtils.removeTableRows(this._gui.mains, 2);
  DlmanageUtils.clear(this._gui.dateZone);

  var twedcus = 0, twedtol = 0, tprecus = 0, tpretol = 0, tporcus = 0, tportol = 0,  tengcus = 0, tengtol = 0, tothcus = 0, tothtol = 0, ttolcus = 0, ttoltol = 0;
  for (var i = 0, il = this._customers.length; i < il; i++) {
    var month = this._customers[i];
    tr = this._gui.mains.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td11';
    td.appendChild(document.createTextNode(DlmanageUtils.monthChange(month.month) + '-' + String(month.year).substring(4, 2)));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td9';
    td.appendChild(document.createTextNode(month.wedcus));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td10';
    td.appendChild(document.createTextNode('$' + parseFloat(month.wedtol, 10).toFixed(2)));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td9';
    td.appendChild(document.createTextNode(month.precus));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td10';
    td.appendChild(document.createTextNode('$' + parseFloat(month.pretol, 10).toFixed(2)));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td9';
    td.appendChild(document.createTextNode(month.porcus));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td10';
    td.appendChild(document.createTextNode('$' + parseFloat(month.portol, 10).toFixed(2)));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td9';
    td.appendChild(document.createTextNode(month.engcus));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td10';
    td.appendChild(document.createTextNode('$' + parseFloat(month.engtol, 10).toFixed(2)));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td9';
    td.appendChild(document.createTextNode(month.othcus));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td10';
    td.appendChild(document.createTextNode('$' + parseFloat(month.othtol, 10).toFixed(2)));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td9';
    td.appendChild(document.createTextNode(month.tolcus));
    td = tr.insertCell(-1);
    td.className = 'elemsummonthlyincome_td10';
    td.appendChild(document.createTextNode('$' + parseFloat(month.toltol, 10).toFixed(2)));
    
    twedcus += parseInt(month.wedcus, 10);
    twedtol += parseFloat(month.wedtol, 10);
    tprecus += parseInt(month.precus, 10);
    tpretol += parseFloat(month.pretol, 10);
    tporcus += parseInt(month.porcus, 10);
    tportol += parseFloat(month.portol, 10);
    tengcus += parseInt(month.engcus, 10);
    tengtol += parseFloat(month.engtol, 10);
    tothcus += parseInt(month.othcus, 10);
    tothtol += parseFloat(month.othtol, 10);
    ttolcus += parseInt(month.tolcus, 10);
    ttoltol += parseFloat(month.toltol, 10);
  }
  tr = this._gui.mains.insertRow(-1);
  tr.className = 'elemsummonthlyincome_tr1';
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td11';
  td.appendChild(document.createTextNode('Total'));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td9';
  td.appendChild(document.createTextNode(twedcus));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td10';
  td.appendChild(document.createTextNode('$' + twedtol.toFixed(2)));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td9';
  td.appendChild(document.createTextNode(tprecus));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td10';
  td.appendChild(document.createTextNode('$' + tpretol.toFixed(2)));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td9';
  td.appendChild(document.createTextNode(tporcus));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td10';
  td.appendChild(document.createTextNode('$' + tportol.toFixed(2)));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td9';
  td.appendChild(document.createTextNode(tengcus));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td10';
  td.appendChild(document.createTextNode('$' + tengtol.toFixed(2)));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td9';
  td.appendChild(document.createTextNode(tothcus));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td10';
  td.appendChild(document.createTextNode('$' + tothtol.toFixed(2)));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td9';
  td.appendChild(document.createTextNode(ttolcus));
  td = tr.insertCell(-1);
  td.className = 'elemsummonthlyincome_td10';
  td.appendChild(document.createTextNode('$' + ttoltol.toFixed(2)));

  /*--DATE ZONE----*/
  this._dateZone = { created: '',
                     to: ''
                   };
  this._gui.dateZone.dateCreated.value = (this._dateZone.created) ? SimpleDate.format(this._dateZone.created) : '';
  this._gui.dateZone.dateCreated.onclick = function() { var context = this;
                                                        _self._changeDate.call(_self, this, _self._dateZone.created, function(sd) { _self._dateZone.created = sd; context.focus(); }, false);
                                                        return false;
                                                      };
  this._gui.dateZone.dateTo.value = (this._dateZone.to) ? SimpleDate.format(this._dateZone.to) : '';
  this._gui.dateZone.dateTo.onclick = function() { var context = this;
                                                   _self._changeDate.call(_self, this, _self._dateZone.to, function(sd) { _self._dateZone.to = sd; context.focus(); }, false);
                                                   return false;
                                                 };
  this._gui.dateZone.create.onclick = function() { if (_self._dateZone.created &&_self._dateZone.to) {
                                                     var date = DateZone.instance();
                                                     date.start = _self._dateZone.created;
                                                     date.end = _self._dateZone.to;
                                                     date.page = _self._options.template;
                                                     new RequestUtils()._mwrite('datezone', [date], [], function(date, params) { _self._retrieveDateZones.call(_self);}, DOMUtils.findPos(this));
                                                   } else {
                                                     window.alert('DATE NOT BE EMPTY');
                                                   }
                                                 };
  var table = document.createElement('table');
  table.style.width = '166px';
  table.cellPadding = 0;
  table.cellSpacing = 0;
  this._gui.dateZone.result.appendChild(table);
  for (var i = 0, il = this._dateZones.length; i < il; i++) {
    var date = this._dateZones[i];
    tr = table.insertRow(-1);
    tr.className = (tr.rowIndex % 2 == 0) ? 'rowodd' : 'roweven';
    td = tr.insertCell(-1);
    td.style.width = '146px';
    td.style.height = '20px';
    td.style.textAlign = 'center';
    var a = document.createElement('a');
    a.href = '#';
    a._date = date;
    a.appendChild(document.createTextNode(SimpleDate.format(date.start) + ' - ' + SimpleDate.format(date.end)));
    a.onclick = function() { _self._search.dateFrom = this._date.start;
                             _self._search.dateTo = this._date.end;
                             _self._retrieveCustomers.call(_self, 1, '1=1', JSON.stringify(this._date.start), JSON.stringify(this._date.end));
                             return false;
                           };
    td.appendChild(a);
    td = tr.insertCell(-1);
    td.style.width = '20px';
    td.style.textAlign = 'center';
    var span = document.createElement('span');
    span.appendChild(document.createTextNode('(x)'));
    span.style.cursor = 'pointer';
    span._date = date;
    span.onclick = function() { new RequestUtils()._mwrite('datezone', [], [this._date], function(date, params) { _self._retrieveDateZones.call(_self);}, null);
                              };
    td.appendChild(span);
  }
  
  this._customerSearch(this._gui, function(condition, from, to) { _self._retrieveCustomers.call(_self, 1, condition, from, to); 
                                                                });
};

HandlePageSumIncome.prototype._customerSearch = function(gui, callbackFunc) {
  var _self = this;
  this._callbackFunc = callbackFunc;

  gui.search.dateCreated.value = (this._search.dateFrom) ? SimpleDate.format(this._search.dateFrom) : SimpleDate.format(SimpleDate.fromStamp(SimpleDate.toStamp(this._now)- 31536000));
  gui.search.dateCreated.onclick = function() { var context = this;
                                                _self._changeDate.call(_self, this, _self._search.dateFrom, function(sd) { _self._search.dateFrom = sd; context.focus(); }, false);
                                                return false;
                                              };
  gui.search.dateCreated.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                    if (code == 13) {
                                                      _self._callbackFunc.call(_self, '1=1', (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null);
                                                    }
                                                  };
  gui.search.dateTo.value = (this._search.dateTo) ? SimpleDate.format(this._search.dateTo) : SimpleDate.format(this._now);
  gui.search.dateTo.onclick = function() { var context = this;
                                           _self._changeDate.call(_self, this, _self._search.dateTo, function(sd) { _self._search.dateTo = sd; context.focus(); }, false);
                                           return false;
                                         };
  gui.search.dateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                               if (code == 13) {
                                                 _self._callbackFunc.call(_self, '1=1', (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null);
                                               }
                                             };
 gui.search.search.onclick = function() { _self._callbackFunc.call(_self, '1=1', (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null); };
};

HandlePageSumIncome.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

HandlePageSumIncome.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

HandlePageSumIncome.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};